package it.gommonauti.quiz.patente.app;


import it.gommonauti.patenti.dao.MainQuestionDAO;
import it.gommonauti.patenti.dao.factory.DAOFactory;
import it.gommonauti.patenti.entity.Exam;
import it.gommonauti.patenti.exception.ApplicationException;

import java.sql.SQLException;
import java.util.ArrayList;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * 
 * @author Francesco Stallone ( f.stallone73@gmail.com ).
 *
 * The exams list activity.
 */
public class ChooseExamActivity extends BaseActivity {

	private static final String DEBUG_TAG= "ChooseExamActivity";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {

		Log.i(DEBUG_TAG, "onCreate()");

		super.onCreate(savedInstanceState);
		
		String idLanguage = getSelectedLanguageID();

		ArrayList<Exam> exams = getExams(idLanguage);
		
		ScrollView mainScrollView = new ScrollView(this);
		LinearLayout mainLinearLayout = new LinearLayout(this);
		mainLinearLayout.setOrientation(LinearLayout.VERTICAL);
		mainScrollView.addView(mainLinearLayout);
		this.setContentView(mainScrollView);
		
		if(exams != null){
			
			for (Exam exam : exams) {
				
				// TODO : esternalize
				// Gommostyle :)
				Typeface tf = Typeface.createFromAsset(getAssets(), "ASTRO867.TTF");
				
				Button button = new Button(this);
				button.setText(exam.getDescription());
				button.setTypeface(tf);
				
				//TODO : esternalize
				button.setTextColor(Color.rgb(3, 96, 176));
				
				mainLinearLayout.addView(button);
				
				button.setEnabled(exam.isEnabled());
				
				button.setTag(new Integer(exam.getId()));
				button.setOnClickListener(buttonOnClickHandler);
				
			}
			
		} else {
			
			TextView textView = new TextView(this);
			textView.setText("SORRY: NO LEVEL IN DB");
			mainLinearLayout.addView(textView);
			
		}
		

	}
	
	/**
	 * Button on click listener.
	 */
	private View.OnClickListener buttonOnClickHandler = new View.OnClickListener() {
		public void onClick(View v) {

			onButtonClick(v);

		}	
	};
	
	private void onButtonClick(View v) {
		
		finish();

		Integer selectedExamID = (Integer) v.getTag(); 				
		setSelectedExamID(selectedExamID.intValue());
		
		Intent intent = new Intent();
		intent.setClass(ChooseExamActivity.this, ChooseQuizActivity.class);
		startActivity(intent);	
		
		
	}	
	
	protected ArrayList<Exam> getExams(String idLanguage)  {

		ArrayList<Exam> levels = null;
		
		try{


			// create the required DAO Factory
			DAOFactory h2dbFactory =   
					DAOFactory.getDAOFactory(DAOFactory.H2_DB);

			// Create a DAO
			MainQuestionDAO mainQuestionDAO = h2dbFactory.getMainQuestionDAO();

			levels = mainQuestionDAO.getExams(idLanguage);

		} catch (SQLException e) {
			e.printStackTrace();
		} catch (ApplicationException e) {
			System.out.println(e.getTypes());
		}
		return levels;
	}	
	
	@Override
	protected String getDebugTag() {
		return DEBUG_TAG;
	}		
	
}